// Copyright 2011 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

TILE_CONFIGS = {
  12: {
    xn: 2, y0: 2047, x0: 2047, yn: 2,
    bits: [15]
  },
  13: {
    xn: 2, y0: 4095, x0: 4095, yn: 2,
    bits: [15]
  },
  14: {
    xn: 2, y0: 8190, x0: 8191, yn: 4,
    bits: [255]
  },
  15: {
    xn: 4, y0: 16381, x0: 16382, yn: 6,
    bits: [16777215]
  },
  16: {
    xn: 8, y0: 32763, x0: 32764, yn: 10,
    bits: [4294705151, 4294934495, 65519]
  },
  17: {
    xn: 16, y0: 65527, x0: 65528, yn: 18,
    bits: [
      1862795263, 4217634712, 3730830751, 4177525567, 268420095,
      2138968063, 4254334828, 2566911599, 4294959615
    ]
  },
  18: {
    xn: 32, y0: 131054, x0: 131056, yn: 35,
    bits: [
      4294967295, 130023439, 1673527392, 62915328, 3955234822, 4225810465,
      3280339839, 2217751551, 562249727, 202637310, 1636302833, 406847432,
      2181037659, 124780249, 54523592, 419346112, 4294966272, 4294950927,
      4251844735, 4218945585, 3680502175, 3640659455, 3422683122, 807403408,
      3254779014, 268432432, 4294943106, 4292807704, 4193280103, 2112029183,
      1008207485, 2160080414, 100714383, 4026785856, 4294967295
    ]
  }
};

BASE_MAP = new google.maps.ImageMapType({
  getTileUrl: function (coord, zoom) {
    var config = TILE_CONFIGS[zoom];
    if (config) {
      var tx = coord.x;
      var ty = (1 << zoom) - 1 - coord.y;
      var x = tx - config.x0;
      var y = ty - config.y0;
      if (x >= 0 && x < config.xn && y >= 0 && y < config.yn) {
        var index = x * config.yn + y;
        if ((config.bits[index >> 5] >> (index%32)) & 1) {
          return 'tiles/' + zoom + '/' + tx + '/' + ty + '.png';
        }
      }
    }
    return 'tiles/blank.png';
  },
  tileSize: new google.maps.Size(256, 256),
  isPng: true,
  minZoom: 12,
  maxZoom: 18,
  radius: 123123,
  name: 'BRC'
});
